package com.getpebble.android.main.sections.appstore.constants;

import android.provider.Settings.Secure;
import android.text.TextUtils;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.R;
import com.getpebble.android.common.model.PblDeviceModel.PblDeviceRecord;
import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;

public final class AppStoreConstants {

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$main$sections$appstore$constants$AppStoreConstants$StoreType = new int[StoreType.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$main$sections$appstore$constants$AppStoreConstants$StoreType[StoreType.WATCH_APPS.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$main$sections$appstore$constants$AppStoreConstants$StoreType[StoreType.WATCH_FACES.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$main$sections$appstore$constants$AppStoreConstants$StoreType[StoreType.APPLICATION.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
            try {
                $SwitchMap$com$getpebble$android$main$sections$appstore$constants$AppStoreConstants$StoreType[StoreType.DEVELOPER_APPS.ordinal()] = 4;
            } catch (NoSuchFieldError e4) {
            }
            try {
                $SwitchMap$com$getpebble$android$main$sections$appstore$constants$AppStoreConstants$StoreType[StoreType.ONBOARDING_MIGRATE.ordinal()] = 5;
            } catch (NoSuchFieldError e5) {
            }
            try {
                $SwitchMap$com$getpebble$android$main$sections$appstore$constants$AppStoreConstants$StoreType[StoreType.GET_SOME_APPS.ordinal()] = 6;
            } catch (NoSuchFieldError e6) {
            }
        }
    }

    public enum StoreType {
        WATCH_FACES,
        WATCH_APPS,
        APPLICATION,
        DEVELOPER_APPS,
        ONBOARDING_MIGRATE,
        GET_SOME_APPS,
        UNSUPPORTED
    }

    public static String getStoreUrl(StoreType storeType, String id) {
        String storeUrl = null;
        switch (AnonymousClass1.$SwitchMap$com$getpebble$android$main$sections$appstore$constants$AppStoreConstants$StoreType[storeType.ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
                storeUrl = PebbleApplication.getBootConfig().getAppStoreWatchAppsUrl();
                break;
            case SMTPMessage.RETURN_HDRS /*2*/:
                storeUrl = PebbleApplication.getBootConfig().getAppStoreWatchFacesUrl();
                break;
            case ListInfo.INDETERMINATE /*3*/:
                storeUrl = PebbleApplication.getBootConfig().getAppStoreApplicationUrl();
                break;
            case SMTPMessage.NOTIFY_DELAY /*4*/:
                storeUrl = PebbleApplication.getBootConfig().getAppStoreDeveloperAppsUrl();
                break;
            case R.styleable.WalletFragmentStyle_maskedWalletDetailsHeaderTextAppearance /*5*/:
                storeUrl = PebbleApplication.getBootConfig().getOnboardingMigrateUrl();
                break;
            case R.styleable.WalletFragmentStyle_maskedWalletDetailsBackground /*6*/:
                storeUrl = PebbleApplication.getBootConfig().getOnboardingGetSomeAppsUrl();
                break;
        }
        if (TextUtils.isEmpty(storeUrl)) {
            return storeUrl;
        }
        storeUrl = urlWithConfiguredPlaceholders(storeUrl);
        if (TextUtils.isEmpty(id)) {
            return storeUrl;
        }
        return substitutePlaceholderInUrl(storeUrl, "id", id);
    }

    private static String urlWithConfiguredPlaceholders(String url) {
        String pebble_id;
        String accessToken = PebbleApplication.getSessionManager().peekAuthToken();
        PblDeviceRecord connectedDevice = PebbleApplication.getConnectedDeviceRecord();
        if (connectedDevice == null) {
            pebble_id = "";
        } else {
            pebble_id = connectedDevice.serialNumber;
        }
        return substitutePlaceholderInUrl(substitutePlaceholderInUrl(substitutePlaceholderInUrl(url, "access_token", accessToken), "pebble_id", pebble_id), "phone_id", Secure.getString(PebbleApplication.getAppContext().getContentResolver(), "android_id"));
    }

    private static String substitutePlaceholderInUrl(String url, String key, String value) {
        String placeholder = String.format("$$%s$$", new Object[]{key});
        if (value == null) {
            value = "";
        }
        if (TextUtils.isEmpty(url) || !url.contains(placeholder)) {
            return url;
        }
        return url.replace(placeholder, value);
    }
}
